package com.itfenghuang.day05.service.imp;

import com.itfenghuang.bean.Student;
import com.itfenghuang.day05.mapper.StudentMapper;
import com.itfenghuang.day05.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImp implements StudentService {
    @Override
    public List<Student> selectStusBySids(List<Integer> sids) {
        InputStream is=null;
        SqlSession sqlSession=null;
        List<Student> list=null;
        try {
            //1.获取核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2.获取sqlsession工厂对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3.获取sqlsession对象
            sqlSession = factory.openSession();
            //4.获取对应接口实现类
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.获取方法
            list = mapper.selectStusBySids(sids);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public List<Student> selectAllStus() {
        InputStream is=null;
        SqlSession sqlSession=null;
        List<Student> list=null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory fatory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = fatory.openSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            list = mapper.selectAllStus();
            //必须处理结果或者返回结果
//            System.out.println(list);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    @Test
    public List<Student> selectAllStusAndCla() {
        //1.获取核心配置文件
        InputStream  is = null;
        SqlSession sqlSession=null;
        List<Student> students=null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2.获取sqlsession工厂对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3.获取sqlsession对象
            sqlSession = factory.openSession();
            //4.获取对应接口实现类
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //5.获取方法
            students = mapper.selectAllStusAndCla();
            System.out.println(students);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
